package com.example.lets;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.github.ksoichiro.android.observablescrollview.ObservableListView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2015/11/15.
 */
public class UserSearchFragment extends BaseFragment {

    public static final String ARG_INITIAL_POSITION = "ARG_INITIAL_POSITION";
    public static Context context = null;
    public ArrayList<HashMap<String, Object>> listItem;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_listview, container, false);

        Activity parentActivity = getActivity();
        final ObservableListView listView = (ObservableListView) view.findViewById(R.id.scroll);
        listView.addHeaderView(inflater.inflate(R.layout.padding, listView, false));

        final String inputStr = SearchActivity.et.getText().toString();
        String newInputStr = SearchActivity.ruleSearchString(inputStr);

        final ArrayList<User> retUser = new ArrayList<User>();
        retUser.clear();

        if (inputStr.equals("")) listView.setAdapter(new UserListAdapter(context,  new ArrayList<HashMap<String, Object>>()));
        else {
            BmobQuery<User> nicknameQuery = new BmobQuery<User>();
            BmobQuery<User> tagQuery1 = new BmobQuery<User>();
            BmobQuery<User> tagQuery2 = new BmobQuery<User>();
            BmobQuery<User> tagQuery3 = new BmobQuery<User>();
            BmobQuery<User> userQuery = new BmobQuery<User>();

            nicknameQuery.setLimit(100);
            nicknameQuery.addWhereMatches("nickname", "(.)*" + newInputStr + "(.)*");
            tagQuery1.setLimit(100);
            tagQuery2.setLimit(100);
            tagQuery3.setLimit(100);
            tagQuery1.addWhereMatches("tag1", "(.)*" + newInputStr + "(.)*");
            tagQuery2.addWhereMatches("tag2", "(.)*" + newInputStr + "(.)*");
            tagQuery3.addWhereMatches("tag3", "(.)*" + newInputStr + "(.)*");
            List<BmobQuery<User>> userQueries = new ArrayList<BmobQuery<User>>();
            userQueries.add(tagQuery1);
            userQueries.add(tagQuery2);
            userQueries.add(tagQuery3);
            userQueries.add(nicknameQuery);
            userQuery.or(userQueries);
            userQuery.findObjects(context, new FindListener<User>() {
                @Override
                public void onSuccess(List<User> nicknameResult) {
                    for (User user : nicknameResult) {
                        if (user.getNickname().equals(inputStr))
                            retUser.add(0, user);
                        else
                            retUser.add(user);
                    }
                    listItem = new ArrayList<HashMap<String, Object>>();
                    for (User user : retUser) {
                        HashMap<String, Object> map = new HashMap<String, Object>();

                        map.put("ItemName", user.getNickname());
                        map.put("ItemPicFileName", user.getPicFilename());
                        map.put("ItemId", user.getObjectId());
                        map.put("ItemTag1", user.getTag1());
                        map.put("ItemTag2", user.getTag2());
                        map.put("ItemTag3", user.getTag3());

                        listItem.add(map);
                    }
                    listView.setAdapter(new UserListAdapter(context, listItem));
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            /*Toast.makeText(context, "个人页面正在建设中，请关注后续版本:)", Toast.LENGTH_SHORT).show();*/
                            Intent intent = new Intent();
                            intent.putExtra("username", listItem.get(i - 1).get("ItemName").toString());
                            intent.putExtra("userid", listItem.get(i - 1).get("ItemId").toString());
                            intent.putExtra("userpicname", listItem.get(i - 1).get("ItemPicFileName").toString());
                            intent.setClass(context, UserPageActivity.class);
                            startActivity(intent);
                        }
                    });
                }

                @Override
                public void onError ( int i, String s){

                }
            });
        }

        if (parentActivity instanceof ObservableScrollViewCallbacks) {
            // Scroll to the specified position after layout
            Bundle args = getArguments();
            if (args != null && args.containsKey(ARG_INITIAL_POSITION)) {
                final int initialPosition = args.getInt(ARG_INITIAL_POSITION, 0);
                ScrollUtils.addOnGlobalLayoutListener(listView, new Runnable() {
                    @Override
                    public void run() {
                        // scrollTo() doesn't work, should use setSelection()
                        listView.setSelection(initialPosition);
                    }
                });
            }

            // TouchInterceptionViewGroup should be a parent view other than ViewPager.
            // This is a workaround for the issue #117:
            // https://github.com/ksoichiro/Android-ObservableScrollView/issues/117
            listView.setTouchInterceptionViewGroup((ViewGroup) parentActivity.findViewById(R.id.root));

            listView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity);
        }
        return view;
    }
}
